//
//  CKPuslichTool.m
//  chengKuanDriver
//
//  Created by admin on 16/3/22.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CKPuslichTool.h"

@implementation CKPuslichTool
+(UIViewController *)getViewControllerFromStroyBoard:(NSString *)storyName viewID:(NSString *)viewID{
    UIStoryboard *story = [UIStoryboard storyboardWithName:storyName bundle:[NSBundle mainBundle]];
    UIViewController *viewController = [story instantiateViewControllerWithIdentifier:viewID];
    return viewController;
}
/**
 *  获取带虚线边框的图片
 *
 *  @param
 */
+(UIImage*)imageWithSize:(CGSize)size borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [[UIColor clearColor] set];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, borderWidth);
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGFloat lengths[] = { 3, 1 };
    CGContextSetLineDash(context, 0, lengths, 1);
    CGContextMoveToPoint(context, 0.0, 0.0);
    CGContextAddLineToPoint(context, size.width, 0.0);
    CGContextAddLineToPoint(context, size.width, size.height);
    CGContextAddLineToPoint(context, 0, size.height);
    CGContextAddLineToPoint(context, 0.0, 0.0);
    CGContextStrokePath(context);
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
//等比缩小
+(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    /*获取当前屏幕的比例*/  //一个点的像素
    float scale = [UIScreen mainScreen].scale;
    //opaque  :alpha通道  Yes:不透明/NO 透明  一般用NO  清晰
    /*开启位图上下文*/ //size 为CGSize类型，即你所需要的图片尺寸
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, scale);
    
    [image drawInRect:CGRectMake(0, 0, size.width+10, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}


//等比缩小
+(UIImage *) imageWithImagePaperSimple:(UIImage*)image NScale:(float) nScale  maxW:(float)max{
    //计算当前的应该显示的宽度宽度
    float maxW_curremt = image.size.width *nScale;
    float maxH_curremt = image.size.height *nScale;
    
    if (maxW_curremt > max) {
        maxW_curremt = max;
        maxH_curremt = maxW_curremt *image.size.height /image.size.width;
    }
    
    
    /*获取当前屏幕的比例*/  //一个点的像素
    float scale = [UIScreen mainScreen].scale;
    //opaque  :alpha通道  Yes:不透明/NO 透明  一般用NO  清晰
    /*开启位图上下文*/
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(maxW_curremt, maxH_curremt), NO, scale);
    //    UIGraphicsBeginImageContext(newSize);  //size 为CGSize类型，即你所需要的图片尺寸
    
    [image drawInRect:CGRectMake(0, 0, maxW_curremt, maxH_curremt)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    /*结束位图编辑*/
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}
#pragma mark 创建一个圆形带有边框的图片修发图片大小
+(UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize{
    newSize.height=image.size.height*(newSize.width/image.size.width);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
}
#pragma mark 创建一个圆形带有边框的图片
+(UIImage *)addSumImage:(UIImage *)image borderColor:(UIColor *)borderColor borderWith:(float )borderWith{
    
    
    /*获取图片大小*/
    float imageW = image.size.width;
    float imageH = image.size.height;
    /*获取当前屏幕的比例*/  //一个点的像素
    float scale = [UIScreen mainScreen].scale;
    
    //opaque  :alpha通道  Yes:不透明/NO 透明  一般用NO  清晰
    /*开启位图上下文*/
    UIGraphicsBeginImageContextWithOptions(image.size, NO, scale);
    
    
    
    /**开启上下文
     */
    CGContextRef context =  UIGraphicsGetCurrentContext();
    
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, imageW, imageH));
    
    CGContextClip(context);
    
    //画图
    [image drawInRect:CGRectMake(0, 0, imageW, imageH)];
    
    
    
    /**设置边框的宽度  添加边框*/
    
    CGContextSetLineWidth(context, borderWith);
    //设置边框颜色
    [borderColor set];
    //添加边框
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, imageW, imageH));
    CGContextStrokePath(context);
    
    
    /*获取当前上下文的图片**/
    
    UIImage *backImg = UIGraphicsGetImageFromCurrentImageContext();
    
    
    /*结束位图编辑*/
    
    UIGraphicsEndImageContext();
    
    return backImg;
}
#pragma mark 创建一个圆形带有边框的图片  控制大小
+(UIImage *)addSumImage:(UIImage *)image borderColor:(UIColor *)borderColor borderWith:(float )borderWith
                   size:(CGSize )size{
    
    
    /*获取图片大小*/
    float imageW = size.width;
    float imageH = size.height;
    /*获取当前屏幕的比例*/  //一个点的像素
    float scale = [UIScreen mainScreen].scale;
    
    //opaque  :alpha通道  Yes:不透明/NO 透明  一般用NO  清晰
    /*开启位图上下文*/
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    
    
    
    /**开启上下文
     */
    CGContextRef context =  UIGraphicsGetCurrentContext();
    
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, imageW, imageH));
    
    CGContextClip(context);
    
    //画图
    [image drawInRect:CGRectMake(0, 0, imageW, imageH)];
    
    
    
    /**设置边框的宽度  添加边框*/
    
    CGContextSetLineWidth(context, borderWith);
    //设置边框颜色
    [borderColor set];
    //添加边框
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, imageW, imageH));
    CGContextStrokePath(context);
    
    
    /*获取当前上下文的图片**/
    
    UIImage *backImg = UIGraphicsGetImageFromCurrentImageContext();
    
    
    /*结束位图编辑*/
    
    UIGraphicsEndImageContext();
    
    return backImg;
}

#pragma mark - 设置targetView的圆角 ＋ 边线宽 ＋ 边线颜色
void UIViewSetRadius(UIView *targetView, CGFloat radius, CGFloat borderWidth, UIColor *borderColor){
    targetView.layer.cornerRadius = radius;
    if (borderWidth > 0) {
        targetView.layer.borderWidth  = borderWidth;
    }
    if (borderColor) {
        targetView.layer.borderColor  = borderColor.CGColor;
    }
    targetView.clipsToBounds      = YES;
}

#pragma mark -字符串判空
+(BOOL)isNilOrNull:(id)obj{
    BOOL isNull = NO;
    
    if (obj == nil || [obj isKindOfClass:[NSNull class]]) {
        isNull = YES;
    }
    else {
        if ([obj isKindOfClass:[NSString class]] || [obj isKindOfClass:[NSMutableString class]]) {
            if ([[(NSString *)obj stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
                isNull = YES;
            }
        }
    }
    
    return isNull;
}

#pragma mark -对分割线处理
+(void)configRealTableView:(UITableView *)tableview{
    tableview.separatorColor = [UIColor colorWithRed:0xcc/255.0 green:0xcc/255.0 blue:0xcc/255.0 alpha:1.0];
    if ([tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableview setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableview setLayoutMargins:UIEdgeInsetsZero];
    }
    
    //清除底部多余的分割线
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableview setTableFooterView:view];
}
#pragma mark -将文件size转成KB、MB、GB
/**
 系统自带方法：[NSByteCountFormatter stringFromByteCount:1999 countStyle:NSByteCountFormatterCountStyleFile];
 其中NSByteCountFormatterCountStyleFile是一个枚举：
 
 typedef NS_ENUM(NSInteger, NSByteCountFormatterCountStyle) {
 // Specifies display of file or storage byte counts. The actual behavior for this is platform-specific; on OS X 10.8, this uses the decimal style, but that may change over time.
 NSByteCountFormatterCountStyleFile   = 0,
 // Specifies display of memory byte counts. The actual behavior for this is platform-specific; on OS X 10.8, this uses the binary style, but that may change over time.
 NSByteCountFormatterCountStyleMemory = 1,
 // The following two allow specifying the number of bytes for KB explicitly. It's better to use one of the above values in most cases.
 NSByteCountFormatterCountStyleDecimal = 2,    // 1000 bytes are shown as 1 KB
 NSByteCountFormatterCountStyleBinary  = 3     // 1024 bytes are shown as 1 KB
 };
 **/
- (id)transformedValue:(id)value{
    double convertedValue = [value doubleValue];
    int multiplyFactor = 0;
    
    NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB", @"ZB", @"YB",nil];
    
    while (convertedValue > 1024) {
        convertedValue /= 1024;
        multiplyFactor++;
    }
    
    return [NSString stringWithFormat:@"%4.2f %@",convertedValue, [tokens objectAtIndex:multiplyFactor]];
}

#pragma mark -旋转图片
+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation

{
    
    long double rotate = 0.0;
    
    CGRect rect;
    
    float translateX = 0;
    
    float translateY = 0;
    
    float scaleX = 1.0;
    
    float scaleY = 1.0;
    
    
    
    switch (orientation) {
            
        case UIImageOrientationLeft:
            
            rotate = M_PI_2;
            
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            
            translateX = 0;
            
            translateY = -rect.size.width;
            
            scaleY = rect.size.width/rect.size.height;
            
            scaleX = rect.size.height/rect.size.width;
            
            break;
            
        case UIImageOrientationRight:
            
            rotate = 3 * M_PI_2;
            
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            
            translateX = -rect.size.height;
            
            translateY = 0;
            
            scaleY = rect.size.width/rect.size.height;
            
            scaleX = rect.size.height/rect.size.width;
            
            break;
            
        case UIImageOrientationDown:
            
            rotate = M_PI;
            
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            translateX = -rect.size.width;
            
            translateY = -rect.size.height;
            
            break;
            
        default:
            
            rotate = 0.0;
            
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            translateX = 0;
            
            translateY = 0;
            
            break;
            
    }
    
    //
    //    /*获取当前屏幕的比例*/  //一个点的像素
    float scale = [UIScreen mainScreen].scale;
    UIImage * newPic = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:orientation];
    //    //opaque  :alpha通道  Yes:不透明/NO 透明  一般用NO  清晰
    //    /*开启位图上下文*/
    //    UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
    ////    UIGraphicsBeginImageContext(rect.size);
    //
    //    CGContextRef context = UIGraphicsGetCurrentContext();
    //
    //    //做CTM变换
    //
    //    CGContextTranslateCTM(context, 0.0, rect.size.height);
    ////
    //    CGContextScaleCTM(context, 1.0, -1.0);
    ////
    //    CGContextRotateCTM(context, rotate);
    ////
    //    CGContextTranslateCTM(context, translateX, translateY);
    //
    //
    //
    //    CGContextScaleCTM(context, scaleX, scaleY);
    //
    //    //绘制图片
    //
    //    CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
    //
    //    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    //
    //    /*结束位图编辑*/
    //
    //    UIGraphicsEndImageContext();
    
    return newPic;
    
}


// 判断文件是否存在
+ (BOOL)isFileExistsAtPath:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:filePath];
}

// 判断目录是否存在
+ (BOOL)isFileExistsAtPath:(NSString *)filePath isDirectory:(BOOL *)isDirectory
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:filePath isDirectory:isDirectory];
}



// 删除临时文件夹中所有的文件
+ (void)deleteAllFilesOfTempDirectory
{
    NSLog(@"TOOLS: deleteAllFilesOfTempDirectory");
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *filesArray = [fileManager contentsOfDirectoryAtPath:NSTemporaryDirectory() error:nil];
    
    if ([filesArray count] > 0)
    {
        NSString *fileName = nil;
        for (int i = 0; i < [filesArray count]; i++)
        {
            fileName = [filesArray objectAtIndex: i];
            [fileManager removeItemAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent: fileName] error:nil];
        }
    }
}

// 删除本地存储的文件或文件夹
+ (void)deleteFileOrDirectoryForPath:(NSString *)pathString
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:pathString])
    {
        NSLog(@"TOOLS: deleteFileOrDirectoryForPath: pathString = %@", pathString);
        [fileManager removeItemAtPath:pathString error:nil];
    }
}

#pragma mark -自定义字符串转化为时间//yyyy-MM-dd HH:mm:ss
+ (NSDate*)dateFromCustomString:(NSString*)str format:(NSString*)formatStr{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:formatStr];
    return [format dateFromString:str];
}
#pragma mark -时间转化为自定义字符串
+ (NSString*)stringFromCustomFormatDate:(NSDate*)date format:(NSString*)formatStr{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:formatStr];
    return [format stringFromDate:date];
}

@end
